#include "GUI.h"
#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
//
// Recommended memory to run the sample with adequate performance
//
#define ID_KNOB_0 (GUI_ID_USER + 1)
#define ID_WIN_0 (GUI_ID_USER + 2)
#define RGB24(R,G,B) ((int)(((R))|((G)<<8)|(B)<<16))

#define BK_COLOR   RGB24(0x00,0x96,0x88)//RGB24(16, 36, 49) RGB24(0x0d,0x47,0xa1)
#define AA_MAG     3
#define RSTEP      5
/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       Scale Points 4x4 (only alpha channel)
*/
static GUI_CONST_STORAGE unsigned char _acscale_trans_4x4[] = {
	/* ABS: 012 Pixels @ 000,000 */ 0, 16, 0xFF, 0xBE, 0xA2, 0xFF, 0xBE, 0x00, 0x00, 0x48, 0xA2, 0x00, 0x00, 0x37, 0xFF, 0x70, 0x70, 0xBE,
	0
};
GUI_CONST_STORAGE GUI_BITMAP bmscale_trans_4x4 = {
	4, // xSize
	4, // ySize
	4, // BytesPerLine
	GUI_COMPRESS_RLE8, // BitsPerPixel
	(unsigned char *)_acscale_trans_4x4,  // Pointer to picture data
	NULL,  // Pointer to palette
	GUI_DRAW_RLEALPHA
};

GUI_MEMDEV_Handle colorMem = 0;
/*********************************************************************
*
*       _aDialog
*
* Function description
*   Dialog resource using a WINDOW widget
*/
static const GUI_WIDGET_CREATE_INFO _aDialog[] = {
	{ WINDOW_CreateIndirect, "", ID_WIN_0, 0, 0, 240, 320, WM_CF_SHOW, 0, 0 },
	{ TEXT_CreateIndirect, "Dialog", 0, 80, 5, 100, 20, TEXT_CF_HCENTER },
	{ BUTTON_CreateIndirect, "Close", GUI_ID_BUTTON0, 80, 220, 100, 20, 0 },
	{ KNOB_CreateIndirect, "", ID_KNOB_0, 30, 20, 180, 180, WM_CF_SHOW }
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
GUI_COLOR getColorByBaiFenBi(int bili){  
	float one = (255 + 255) / 100;
	int r = 0;
	int g = 0;
	int b = 0;

	if (bili < 50) {
		r = one * bili;
		g = 255;
	}
	if (bili >= 50) {  
		g = 255 - ((bili - 50) * one);
		r = 255;
	}
	
	return RGB24(r,g,b);
}

static void _DrawKnob(GUI_MEMDEV_Handle hMem, GUI_COLOR Color) {
	int xSizeMem, ySizeMem;

	xSizeMem = GUI_MEMDEV_GetXSize(hMem);
	ySizeMem = GUI_MEMDEV_GetYSize(hMem);
	GUI_AA_PreserveTrans(1);
	GUI_MEMDEV_Select(hMem);
	GUI_SetBkColor(GUI_TRANSPARENT);
	GUI_Clear();
	GUI_SetColor(Color);
	GUI_AA_FillCircle(xSizeMem / 2 + xSizeMem / 3, ySizeMem / 2, 3);
	GUI_MEMDEV_Select(0);
	GUI_AA_PreserveTrans(0);
}

static void _DrawScale(KNOB_Handle hKnob) {
	GUI_COLOR mycolor;

	I32       Angle, Value;
	int       xPos, yPos;
	int start = 360 - RSTEP, end = 360;
	int i= 0,count;
	Value = 360 - KNOB_GetValue(hKnob) / 10;
	xPos = 30 + 180 / 2;
	yPos = 20 + 180 / 2;
	count = Value/5;
	GUI_AA_EnableHiRes();
	GUI_AA_SetFactor(AA_MAG);
	GUI_SetPenSize(20);
	GUI_SetColor(RGB24(0x00, 0x4d, 0x40));//0x38, 0x8e, 0x3c
	GUI_AA_DrawArc(AA_MAG * (xPos), AA_MAG * (yPos),
			AA_MAG * (80), AA_MAG * (80), 0, 360);

	for(;i<count;i++){
		int bili = i * 100 / 72;
		mycolor = getColorByBaiFenBi(bili);
		GUI_SetColor(mycolor);
		GUI_AA_DrawArc(AA_MAG * (xPos), AA_MAG * (yPos),
				AA_MAG * (80), AA_MAG * (80), start-1, end);
		end = start;
		start = start - RSTEP;
	}
	GUI_AA_DisableHiRes();
	GUI_SetColor(GUI_BLACK);
	GUI_DispDecAt(Value,xPos-3,yPos-3,3);
}

static int _GetScale(KNOB_Handle hKnob) {
	I32 Value;

	Value = ((3600 - KNOB_GetValue(hKnob))) / 36;
	return Value;
}

/*********************************************************************
*
*       _cbDialog
*
* Function description
*   Callback routine of the dialog
*/

static void _cbDialog(WM_MESSAGE *pMsg) {
	int NCode;
	int Id;
	int Scale;
	KNOB_Handle  hKnob;
    static GUI_MEMDEV_Handle   hMemKnobSmall;
	static GUI_COLOR           ColorKnob = GUI_BLACK;
	WM_HWIN hWin = pMsg->hWin;
	GUI_RECT                   Rect;
	switch (pMsg->MsgId) {
	case WM_PAINT:
		GUI_SetBkColor(BK_COLOR);
		GUI_Clear();
		hKnob = WM_GetDialogItem(hWin, ID_KNOB_0);
		_DrawScale(hKnob);
		break;
	case WM_INIT_DIALOG:
		hMemKnobSmall = GUI_MEMDEV_CreateFixed32(0, 0, 70, 20);
		_DrawKnob(hMemKnobSmall, ColorKnob);
		hKnob = WM_GetDialogItem(hWin, ID_KNOB_0);
		KNOB_SetDevice(hKnob, hMemKnobSmall);
		KNOB_SetRange(hKnob, 0, 3600);
		KNOB_SetOffset(hKnob, 0);
		KNOB_SetPeriod(hKnob, 500);
		KNOB_SetPos(hKnob,3600);
		break;
	case WM_NOTIFY_PARENT:
		Id = WM_GetId(pMsg->hWinSrc);    // Id of widget
		NCode = pMsg->Data.v;               // Notification code
		switch (NCode) {
		case WM_NOTIFICATION_RELEASED:      // React only if released
			switch (Id) {
			case GUI_ID_BUTTON0:
				GUI_EndDialog(pMsg->hWin, 0);
				break;
			}
			break;
		case WM_NOTIFICATION_VALUE_CHANGED:
			switch (Id){
			case ID_KNOB_0:
				hKnob = WM_GetDialogItem(hWin, Id);
				Scale = _GetScale(hKnob);
				
				break;
			}
			break;
		}
		break;
	default:
		WM_DefaultProc(pMsg);
	}
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
	GUI_Init();
	WM_SetCreateFlags(WM_CF_MEMDEV);
	GUI_CreateDialogBox(_aDialog, GUI_COUNTOF(_aDialog), _cbDialog, WM_HBKWIN, 0, 0);
	while (1) {
		GUI_Delay(100);
	}
}